package com.jie.langlangmall.portal.service.impl;

import com.jie.langlangmall.common.constant.MessageConstant;
import com.jie.langlangmall.common.exception.ApiException;
import com.jie.langlangmall.mapper.PmsProductMapper;
import com.jie.langlangmall.model.PmsProduct;
import com.jie.langlangmall.model.UmsMember;
import com.jie.langlangmall.portal.domain.MemberProductCollection;
import com.jie.langlangmall.portal.repository.MemberProductCollectionRepository;
import com.jie.langlangmall.portal.service.MemberCollectionService;
import com.jie.langlangmall.portal.service.UmsMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

/**
 * @Author Jie
 * @Date 2024/11/27
 * @Email 3217998214@qq.com
 * @Description 用户收藏
 */
@Service
public class MemberCollectionServiceImpl implements MemberCollectionService {

    @Autowired
    private PmsProductMapper productMapper;
    @Autowired
    private MemberProductCollectionRepository productCollectionRepository;
    @Autowired
    private UmsMemberService memberService;


    @Override
    public void add(MemberProductCollection productCollection) {
        if (productCollection.getProductId() == null) {
            throw new ApiException(MessageConstant.SUBMIT_PARAM_ERROR);
        }
        UmsMember member = memberService.getCurrentMember();
        productCollection.setMemberId(member.getId());
        productCollection.setMemberNickname(member.getNickname());
        productCollection.setMemberIcon(member.getIcon());
        MemberProductCollection findCollection = productCollectionRepository.findByMemberIdAndProductId(productCollection.getMemberId(), productCollection.getProductId());
        if (findCollection == null) {
            PmsProduct product = productMapper.selectByPrimaryKey(productCollection.getProductId());
            if (product == null || product.getDeleteStatus() == 1) {
                throw new ApiException("收藏的商品不存在");
            }
            productCollection.setProductName(product.getName());
            productCollection.setProductSubTitle(product.getSubTitle());
            productCollection.setProductPrice(product.getPrice() + "");
            productCollection.setProductPic(product.getPic());
            productCollectionRepository.save(productCollection);
        }
    }

    @Override
    public void delete(Long productId) {
        UmsMember member = memberService.getCurrentMember();
        productCollectionRepository.deleteByMemberIdAndProductId(member.getId(), productId);
    }

    @Override
    public Page<MemberProductCollection> list(Integer pageNum, Integer pageSize) {
        UmsMember member = memberService.getCurrentMember();
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return productCollectionRepository.findByMemberId(member.getId(), pageable);
    }

    @Override
    public MemberProductCollection detail(Long productId) {
        UmsMember member = memberService.getCurrentMember();
        return productCollectionRepository.findByMemberIdAndProductId(member.getId(), productId);
    }

    @Override
    public void clear() {
        UmsMember member = memberService.getCurrentMember();
        productCollectionRepository.deleteAllByMemberId(member.getId());
    }

}
